package org.mclll.xwangplugin.util;

import com.google.common.collect.Maps;
import org.bukkit.DyeColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;

import java.io.File;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * @author: geduo
 * @date: 2023/10/12 23:02
 */
public class GetChineseNameUtil {

    public static final String CONFIG_FILE_NAME = "materialCn.yml";

    private static Map<Material, String> materialNameMap;
    private static Map<EntityType, String> entityNameMap;
    private static Map<DyeColor, String> colorNameMap;

    public static void init(File configFolder) {
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(configFolder, CONFIG_FILE_NAME));
        ConfigurationSection material = config.getConfigurationSection("Material");

        Set<String> keys = material.getKeys(false);
        materialNameMap = Maps.newHashMap();
        for (String key : keys) {
            try {
                materialNameMap.put(Material.valueOf(key), material.getString(key));
            } catch (Exception ignore) {
            }
        }
        ConfigurationSection entity = config.getConfigurationSection("Entity");
        keys = entity.getKeys(false);
        entityNameMap = Maps.newHashMap();
        for (String key : keys) {
            try {
                entityNameMap.put(EntityType.valueOf(key.toUpperCase(Locale.ROOT)), entity.getString(key));
            } catch (Exception ignore) {
            }
        }
        ConfigurationSection color = config.getConfigurationSection("WoolColor");
        keys = color.getKeys(false);
        colorNameMap = Maps.newHashMap();
        for (String key : keys) {
            try {
                colorNameMap.put(DyeColor.valueOf(key.toUpperCase(Locale.ROOT)), color.getString(key));
            } catch (Exception ignore) {
            }
        }
    }

    public static String get(Material m) {
        return materialNameMap.getOrDefault(m, m.name());
    }

    public static String get(EntityType e) {
        return entityNameMap.getOrDefault(e, e.name());
    }
    public static String get(DyeColor e) {
        return colorNameMap.getOrDefault(e, e.name());
    }
}
